package com.hexacta.hrs.sandbox.pr.web;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;

import com.hexacta.hrs.sandbox.pr.model.Person;
import com.hexacta.hrs.sandbox.pr.model.Profile;
import com.hexacta.hrs.sandbox.pr.service.PersonService;
import com.hexacta.hrs.sandbox.pr.service.ProfileService;
import com.opensymphony.xwork2.ActionSupport;

@ContextConfiguration(locations = "classpath:applicationContext.xml")
public class PersonAction extends ActionSupport
{
	private static final long serialVersionUID = 1L;
	private String name;
	private String password;
	private List<Profile> profiles;
	private List<String> checkedProfiles;
	private List<Person> persons;
	@Autowired
	private PersonService personService;
	@Autowired
	private ProfileService profileService;
		
	public List<Person> getPersons() {
		return persons;
	}

	public void setPersons(List<Person> persons) {
		this.persons = persons;
	}

	public List<String> getCheckedProfiles() {
		return checkedProfiles;
	}

	public void setCheckedProfiles(List<String> checkedProfiles) {
		this.checkedProfiles = checkedProfiles;
	}

	public void setProfileService(ProfileService profileService) {
		this.profileService = profileService;
	}
  
	public ProfileService getProfileService() {
		return profileService;
	}

	public void setPersonService(PersonService personService) {
		this.personService = personService;
	}
  
	public PersonService getPersonService() {
		return personService;
	}

	public String getName() {
		return name;
	}
	
	public void setName(String name) {
		this.name = name;
	}
	
	public String getPassword() {
		return password;
	}
	
	public void setPassword(String password) {
		this.password = password;
	}
	
	public List<Profile> getProfiles() {
		return profiles;
	}

	public void setProfiles(List<Profile> profiles) {
		this.profiles = profiles;
	}
	
	public String execute()
	{
	    return SUCCESS;
	}
	
	public String create() {
		//Es el primer pedido, load the view
		this.setProfiles(this.getProfileService().listAll());
		return SUCCESS;
	}
	
	public String save() {
		//Validacion
		this.Personvalidate();
		
		if( this.hasFieldErrors() ) {
			//TODO esto deberia hacerse con validate de struts
			//tengo que llenar otra vez las listas para la vista
			this.setProfiles(this.getProfileService().listAll());
			return "VALIDATE";
		}

		List<Profile> selectedProfiles = new ArrayList<Profile>();
		this.setProfiles(this.getProfileService().listAll());
		for(Profile profile: this.getProfiles()) {
			for( String str : this.getCheckedProfiles() ) {
				if (profile.getId().equals(Long.parseLong(str))) {
					selectedProfiles.add(profile);
				}
			}
		}
		
		this.getPersonService().addPerson(name, password, selectedProfiles);
		
		return "LIST";
	}
	
	public String list() {
		this.setPersons(this.getPersonService().listAll());
		return SUCCESS;
	}
	
	public String remove() {
		return SUCCESS;
	}

	public void Personvalidate() {
		if( this.name.length() > 8 )
			this.addFieldError("name", "Max name length = 8");
		if( this.name.contains(" ") )
			this.addFieldError("name", "Spaces are not allowed");
		if( this.password.length() > 4 )
			this.addFieldError("password", "Max password length = 4");
		if( this.getPersonService().findPersonByName(this.name).size() != 0 )
			this.addFieldError("name", "Name already in use");
	}
}